

<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>cdt.causality.graph.bnlearn &mdash; Causal Discovery Toolbox 0.5.22 documentation</title>
  

  
  <link rel="stylesheet" href="../../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../../../_static/custom.css" type="text/css" />

  
  
    <link rel="shortcut icon" href="../../../../_static/favicon.png"/>
  
  
  

  
  <!--[if lt IE 9]>
    <script src="../../../../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
        <script src="../../../../_static/jquery.js"></script>
        <script src="../../../../_static/underscore.js"></script>
        <script src="../../../../_static/doctools.js"></script>
        <script src="../../../../_static/language_data.js"></script>
        <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
        <script type="text/x-mathjax-config">MathJax.Hub.Config({"extensions": ["tex2jax.js"], "jax": ["input/TeX", "output/HTML-CSS"], "tex2jax": {"inlineMath": [["$", "$"], ["\\(", "\\)"]], "displayMath": [["$$", "$$"], ["\\[", "\\]"]], "processEscapes": true}, "HTML-CSS": {"fonts": ["TeX"]}})</script>
    
    <script type="text/javascript" src="../../../../_static/js/theme.js"></script>

    
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../../index.html">
          

          
            
            <img src="../../../../_static/banner.png" class="logo" alt="Logo"/>
          
          </a>

          
            
            
              <div class="version">
                0.5.22
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        
        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../index.html">Causal Discovery Toolbox Documentation</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../tutorial.html">Get started</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../causality.html">cdt.causality</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../independence.html">cdt.independence</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../data.html">cdt.data</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../utils.html">cdt.utils</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../metrics.html">cdt.metrics</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../settings.html">Toolbox Settings</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../models.html">PyTorch Models</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../developer.html">Developer Documentation</a></li>
</ul>

            
          
        </div>
        
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../../index.html">Causal Discovery Toolbox</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../../index.html" class="icon icon-home"></a> &raquo;</li>
        
          <li><a href="../../../index.html">Module code</a> &raquo;</li>
        
      <li>cdt.causality.graph.bnlearn</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for cdt.causality.graph.bnlearn</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;BN learn algorithms.</span>

<span class="sd">Imported from the bnlearn package.</span>
<span class="sd">Author: Diviyan Kalainathan</span>

<span class="sd">.. MIT License</span>
<span class="sd">..</span>
<span class="sd">.. Copyright (c) 2018 Diviyan Kalainathan</span>
<span class="sd">..</span>
<span class="sd">.. Permission is hereby granted, free of charge, to any person obtaining a copy</span>
<span class="sd">.. of this software and associated documentation files (the &quot;Software&quot;), to deal</span>
<span class="sd">.. in the Software without restriction, including without limitation the rights</span>
<span class="sd">.. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell</span>
<span class="sd">.. copies of the Software, and to permit persons to whom the Software is</span>
<span class="sd">.. furnished to do so, subject to the following conditions:</span>
<span class="sd">..</span>
<span class="sd">.. The above copyright notice and this permission notice shall be included in all</span>
<span class="sd">.. copies or substantial portions of the Software.</span>
<span class="sd">..</span>
<span class="sd">.. THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR</span>
<span class="sd">.. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,</span>
<span class="sd">.. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE</span>
<span class="sd">.. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER</span>
<span class="sd">.. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,</span>
<span class="sd">.. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE</span>
<span class="sd">.. SOFTWARE.</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">uuid</span>
<span class="kn">import</span> <span class="nn">warnings</span>
<span class="kn">import</span> <span class="nn">networkx</span> <span class="k">as</span> <span class="nn">nx</span>
<span class="kn">from</span> <span class="nn">pathlib</span> <span class="kn">import</span> <span class="n">Path</span>
<span class="kn">from</span> <span class="nn">shutil</span> <span class="kn">import</span> <span class="n">rmtree</span>
<span class="kn">from</span> <span class="nn">tempfile</span> <span class="kn">import</span> <span class="n">gettempdir</span>
<span class="kn">from</span> <span class="nn">.model</span> <span class="kn">import</span> <span class="n">GraphModel</span>
<span class="kn">from</span> <span class="nn">pandas</span> <span class="kn">import</span> <span class="n">DataFrame</span><span class="p">,</span> <span class="n">read_csv</span>
<span class="kn">from</span> <span class="nn">...utils.R</span> <span class="kn">import</span> <span class="n">RPackages</span><span class="p">,</span> <span class="n">launch_R_script</span>
<span class="kn">from</span> <span class="nn">...utils.Settings</span> <span class="kn">import</span> <span class="n">SETTINGS</span>


<span class="k">def</span> <span class="nf">message_warning</span><span class="p">(</span><span class="n">msg</span><span class="p">,</span> <span class="o">*</span><span class="n">a</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Ignore everything except the message.&quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span>


<span class="n">warnings</span><span class="o">.</span><span class="n">formatwarning</span> <span class="o">=</span> <span class="n">message_warning</span>


<div class="viewcode-block" id="BNlearnAlgorithm"><a class="viewcode-back" href="../../../../causality.html#cdt.causality.graph.bnlearn.BNlearnAlgorithm">[docs]</a><span class="k">class</span> <span class="nc">BNlearnAlgorithm</span><span class="p">(</span><span class="n">GraphModel</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;BNlearn algorithm. All these models imported from bnlearn revolve around</span>
<span class="sd">    this base class and have all the same attributes/interface.</span>

<span class="sd">    Args:</span>
<span class="sd">        score (str):the label of the conditional independence test to be used in the</span>
<span class="sd">           algorithm. If none is specified, the default test statistic is the mutual information</span>
<span class="sd">           for categorical variables, the Jonckheere-Terpstra test for ordered factors and the</span>
<span class="sd">           linear correlation for continuous variables. See below for available tests.</span>
<span class="sd">        alpha (float): a numeric value, the target nominal type I error rate.</span>
<span class="sd">        beta (int): a positive integer, the number of permutations considered for each permutation</span>
<span class="sd">           test. It will be ignored with a warning if the conditional independence test specified by the</span>
<span class="sd">           score argument is not a permutation test.</span>
<span class="sd">        optim (bool): See bnlearn-package for details.</span>
<span class="sd">        verbose (bool): Sets the verbosity. Defaults to SETTINGS.verbose</span>

<span class="sd">    .. _bnlearntests:</span>

<span class="sd">    Available tests:</span>
<span class="sd">        • discrete case (categorical variables)</span>
<span class="sd">           – mutual information: an information-theoretic distance measure.</span>
<span class="sd">               It&#39;s proportional to the log-likelihood ratio (they differ by a 2n factor)</span>
<span class="sd">               and is related to the deviance of the tested models. The asymptotic χ2 test</span>
<span class="sd">               (mi and mi-adf,  with  adjusted  degrees  of  freedom), the Monte Carlo</span>
<span class="sd">               permutation test (mc-mi), the sequential Monte Carlo permutation</span>
<span class="sd">               test (smc-mi), and the semiparametric test (sp-mi) are implemented.</span>
<span class="sd">           – shrinkage estimator for the mutual information (mi-sh)</span>
<span class="sd">               An improved</span>
<span class="sd">               asymptotic χ2 test based on the James-Stein estimator for the mutual</span>
<span class="sd">               information.</span>
<span class="sd">           – Pearson’s X2 : the classical Pearson&#39;s X2 test for contingency tables.</span>
<span class="sd">               The asymptotic χ2 test (x2 and x2-adf, with adjusted degrees of freedom),</span>
<span class="sd">               the Monte Carlo permutation test (mc-x2), the sequential Monte Carlo</span>
<span class="sd">               permutation test (smc-x2) and semiparametric test (sp-x2) are implemented  .</span>

<span class="sd">        • discrete case (ordered factors)</span>
<span class="sd">           – Jonckheere-Terpstra : a trend test for ordinal variables.</span>
<span class="sd">              The</span>
<span class="sd">              asymptotic normal test (jt), the Monte Carlo permutation test (mc-jt)</span>
<span class="sd">              and the sequential Monte Carlo permutation test (smc-jt) are implemented.</span>

<span class="sd">        • continuous case (normal variables)</span>
<span class="sd">           – linear  correlation:  Pearson’s  linear  correlation.</span>
<span class="sd">               The exact</span>
<span class="sd">               Student’s  t  test  (cor),  the Monte Carlo permutation test (mc-cor)</span>
<span class="sd">               and the sequential Monte Carlo permutation test (smc-cor) are implemented.</span>
<span class="sd">           – Fisher’s Z: a transformation of the linear correlation with asymptotic normal distribution.</span>
<span class="sd">               Used by commercial software (such as TETRAD II)</span>
<span class="sd">               for the PC algorithm (an R implementation is present in the pcalg</span>
<span class="sd">               package on CRAN). The asymptotic normal test (zf), the Monte Carlo</span>
<span class="sd">               permutation test (mc-zf) and the sequential Monte Carlo permutation</span>
<span class="sd">               test (smc-zf) are implemented.</span>
<span class="sd">           – mutual information: an information-theoretic distance measure.</span>
<span class="sd">               Again</span>
<span class="sd">               it is proportional to the log-likelihood ratio (they differ by a 2n</span>
<span class="sd">               factor). The asymptotic χ2 test (mi-g), the Monte Carlo permutation</span>
<span class="sd">               test (mc-mi-g) and the sequential Monte Carlo permutation test</span>
<span class="sd">               (smc-mi-g) are implemented.</span>

<span class="sd">           – shrinkage estimator for the mutual information(mi-g-sh):</span>
<span class="sd">               an improved</span>
<span class="sd">               asymptotic χ2 test based on the James-Stein estimator for the mutual</span>
<span class="sd">               information.</span>

<span class="sd">        • hybrid case (mixed discrete and normal variables)</span>
<span class="sd">           – mutual information: an information-theoretic distance measure.</span>
<span class="sd">               Again</span>
<span class="sd">               it is proportional to the log-likelihood ratio (they differ by a 2n</span>
<span class="sd">               factor). Only the asymptotic χ2 test (mi-cg) is implemented.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">score</span><span class="o">=</span><span class="s1">&#39;NULL&#39;</span><span class="p">,</span> <span class="n">alpha</span><span class="o">=</span><span class="mf">0.05</span><span class="p">,</span> <span class="n">beta</span><span class="o">=</span><span class="s1">&#39;NULL&#39;</span><span class="p">,</span>
                 <span class="n">optim</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">verbose</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Init the model.&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">RPackages</span><span class="o">.</span><span class="n">bnlearn</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ImportError</span><span class="p">(</span><span class="s2">&quot;R Package bnlearn is not available.&quot;</span><span class="p">)</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">BNlearnAlgorithm</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">arguments</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;</span><span class="si">{FOLDER}</span><span class="s1">&#39;</span><span class="p">:</span> <span class="s1">&#39;/tmp/cdt_bnlearn/&#39;</span><span class="p">,</span>
                          <span class="s1">&#39;</span><span class="si">{FILE}</span><span class="s1">&#39;</span><span class="p">:</span> <span class="n">os</span><span class="o">.</span><span class="n">sep</span> <span class="o">+</span> <span class="s1">&#39;data.csv&#39;</span><span class="p">,</span>
                          <span class="s1">&#39;</span><span class="si">{SKELETON}</span><span class="s1">&#39;</span><span class="p">:</span> <span class="s1">&#39;FALSE&#39;</span><span class="p">,</span>
                          <span class="s1">&#39;</span><span class="si">{ALGORITHM}</span><span class="s1">&#39;</span><span class="p">:</span> <span class="kc">None</span><span class="p">,</span>
                          <span class="s1">&#39;</span><span class="si">{WHITELIST}</span><span class="s1">&#39;</span><span class="p">:</span> <span class="n">os</span><span class="o">.</span><span class="n">sep</span> <span class="o">+</span> <span class="s1">&#39;whitelist.csv&#39;</span><span class="p">,</span>
                          <span class="s1">&#39;</span><span class="si">{BLACKLIST}</span><span class="s1">&#39;</span><span class="p">:</span> <span class="n">os</span><span class="o">.</span><span class="n">sep</span> <span class="o">+</span> <span class="s1">&#39;blacklist.csv&#39;</span><span class="p">,</span>
                          <span class="s1">&#39;</span><span class="si">{SCORE}</span><span class="s1">&#39;</span><span class="p">:</span> <span class="s1">&#39;NULL&#39;</span><span class="p">,</span>
                          <span class="s1">&#39;</span><span class="si">{OPTIM}</span><span class="s1">&#39;</span><span class="p">:</span> <span class="s1">&#39;FALSE&#39;</span><span class="p">,</span>
                          <span class="s1">&#39;</span><span class="si">{ALPHA}</span><span class="s1">&#39;</span><span class="p">:</span> <span class="s1">&#39;0.05&#39;</span><span class="p">,</span>
                          <span class="s1">&#39;</span><span class="si">{BETA}</span><span class="s1">&#39;</span><span class="p">:</span> <span class="s1">&#39;NULL&#39;</span><span class="p">,</span>
                          <span class="s1">&#39;</span><span class="si">{VERBOSE}</span><span class="s1">&#39;</span><span class="p">:</span> <span class="s1">&#39;FALSE&#39;</span><span class="p">,</span>
                          <span class="s1">&#39;</span><span class="si">{OUTPUT}</span><span class="s1">&#39;</span><span class="p">:</span> <span class="n">os</span><span class="o">.</span><span class="n">sep</span> <span class="o">+</span> <span class="s1">&#39;result.csv&#39;</span><span class="p">}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">score</span> <span class="o">=</span> <span class="n">score</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">alpha</span> <span class="o">=</span> <span class="n">alpha</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">beta</span> <span class="o">=</span> <span class="n">beta</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">optim</span> <span class="o">=</span> <span class="n">optim</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">verbose</span> <span class="o">=</span> <span class="n">SETTINGS</span><span class="o">.</span><span class="n">get_default</span><span class="p">(</span><span class="n">verbose</span><span class="o">=</span><span class="n">verbose</span><span class="p">)</span>

<div class="viewcode-block" id="BNlearnAlgorithm.orient_undirected_graph"><a class="viewcode-back" href="../../../../causality.html#cdt.causality.graph.bnlearn.BNlearnAlgorithm.orient_undirected_graph">[docs]</a>    <span class="k">def</span> <span class="nf">orient_undirected_graph</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">graph</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Run the algorithm on an undirected graph.</span>

<span class="sd">        Args:</span>
<span class="sd">            data (pandas.DataFrame): DataFrame containing the data</span>
<span class="sd">            graph (networkx.Graph): Skeleton of the graph to orient</span>

<span class="sd">        Returns:</span>
<span class="sd">            networkx.DiGraph: Solution on the given skeleton.</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Building setup w/ arguments.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">arguments</span><span class="p">[</span><span class="s1">&#39;</span><span class="si">{VERBOSE}</span><span class="s1">&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">verbose</span><span class="p">)</span><span class="o">.</span><span class="n">upper</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">arguments</span><span class="p">[</span><span class="s1">&#39;</span><span class="si">{SCORE}</span><span class="s1">&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">score</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">arguments</span><span class="p">[</span><span class="s1">&#39;</span><span class="si">{BETA}</span><span class="s1">&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">beta</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">arguments</span><span class="p">[</span><span class="s1">&#39;</span><span class="si">{OPTIM}</span><span class="s1">&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">optim</span><span class="p">)</span><span class="o">.</span><span class="n">upper</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">arguments</span><span class="p">[</span><span class="s1">&#39;</span><span class="si">{ALPHA}</span><span class="s1">&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">alpha</span><span class="p">)</span>

        <span class="n">cols</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">columns</span><span class="p">)</span>
        <span class="n">data</span><span class="o">.</span><span class="n">columns</span> <span class="o">=</span> <span class="p">[</span><span class="n">i</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">])]</span>
        <span class="n">mapping</span> <span class="o">=</span> <span class="p">{</span><span class="n">j</span><span class="p">:</span> <span class="n">i</span> <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">([</span><span class="s1">&#39;X&#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span>
                                         <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">])],</span> <span class="n">cols</span><span class="p">)}</span>

        <span class="n">graph2</span> <span class="o">=</span> <span class="n">nx</span><span class="o">.</span><span class="n">relabel_nodes</span><span class="p">(</span><span class="n">graph</span><span class="p">,</span> <span class="n">mapping</span><span class="p">)</span>

        <span class="n">whitelist</span> <span class="o">=</span> <span class="n">DataFrame</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">nx</span><span class="o">.</span><span class="n">edges</span><span class="p">(</span><span class="n">graph2</span><span class="p">)),</span> <span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s2">&quot;from&quot;</span><span class="p">,</span> <span class="s2">&quot;to&quot;</span><span class="p">])</span>
        <span class="n">blacklist</span> <span class="o">=</span> <span class="n">DataFrame</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">nx</span><span class="o">.</span><span class="n">edges</span><span class="p">(</span><span class="n">nx</span><span class="o">.</span><span class="n">DiGraph</span><span class="p">(</span><span class="n">DataFrame</span><span class="p">(</span><span class="o">-</span><span class="n">nx</span><span class="o">.</span><span class="n">adj_matrix</span><span class="p">(</span><span class="n">graph2</span><span class="p">,</span> <span class="n">weight</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span><span class="o">.</span><span class="n">todense</span><span class="p">()</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span>
                                                                 <span class="n">columns</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="n">graph2</span><span class="o">.</span><span class="n">nodes</span><span class="p">()),</span>
                                                                 <span class="n">index</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="n">graph2</span><span class="o">.</span><span class="n">nodes</span><span class="p">()))))),</span> <span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s2">&quot;from&quot;</span><span class="p">,</span> <span class="s2">&quot;to&quot;</span><span class="p">])</span>
        <span class="n">results</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_run_bnlearn</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">whitelist</span><span class="o">=</span><span class="n">whitelist</span><span class="p">,</span>
                                   <span class="n">blacklist</span><span class="o">=</span><span class="n">blacklist</span><span class="p">,</span> <span class="n">verbose</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">verbose</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">nx</span><span class="o">.</span><span class="n">relabel_nodes</span><span class="p">(</span><span class="n">nx</span><span class="o">.</span><span class="n">DiGraph</span><span class="p">(</span><span class="n">results</span><span class="p">),</span>
                                    <span class="p">{</span><span class="n">idx</span><span class="p">:</span> <span class="n">i</span> <span class="k">for</span> <span class="n">idx</span><span class="p">,</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">cols</span><span class="p">)})</span>

        <span class="k">except</span> <span class="n">nx</span><span class="o">.</span><span class="n">exception</span><span class="o">.</span><span class="n">NetworkXError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">results</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
                <span class="n">output</span> <span class="o">=</span> <span class="n">nx</span><span class="o">.</span><span class="n">DiGraph</span><span class="p">()</span>
                <span class="n">output</span><span class="o">.</span><span class="n">add_nodes_from</span><span class="p">([</span><span class="s1">&#39;X&#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">])])</span>
                <span class="n">output</span><span class="o">.</span><span class="n">add_edges_from</span><span class="p">(</span><span class="n">results</span><span class="p">)</span>
                <span class="k">return</span> <span class="n">nx</span><span class="o">.</span><span class="n">relabel_nodes</span><span class="p">(</span><span class="n">output</span><span class="p">,</span> <span class="p">{</span><span class="n">i</span><span class="p">:</span> <span class="n">j</span> <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">j</span> <span class="ow">in</span>
                                                 <span class="nb">zip</span><span class="p">([</span><span class="s1">&#39;X&#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span>
                                                      <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">])],</span> <span class="n">cols</span><span class="p">)})</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">e</span></div>

<div class="viewcode-block" id="BNlearnAlgorithm.orient_directed_graph"><a class="viewcode-back" href="../../../../causality.html#cdt.causality.graph.bnlearn.BNlearnAlgorithm.orient_directed_graph">[docs]</a>    <span class="k">def</span> <span class="nf">orient_directed_graph</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">graph</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Run the algorithm on a directed_graph.</span>

<span class="sd">        Args:</span>
<span class="sd">            data (pandas.DataFrame): DataFrame containing the data</span>
<span class="sd">            graph (networkx.DiGraph): Skeleton of the graph to orient</span>

<span class="sd">        Returns:</span>
<span class="sd">            networkx.DiGraph: Solution on the given skeleton.</span>

<span class="sd">        .. warning::</span>
<span class="sd">           The algorithm is ran on the skeleton of the given graph.</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s2">&quot;The algorithm is ran on the skeleton of the given graph.&quot;</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">orient_undirected_graph</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">nx</span><span class="o">.</span><span class="n">Graph</span><span class="p">(</span><span class="n">graph</span><span class="p">))</span></div>

<div class="viewcode-block" id="BNlearnAlgorithm.create_graph_from_data"><a class="viewcode-back" href="../../../../causality.html#cdt.causality.graph.bnlearn.BNlearnAlgorithm.create_graph_from_data">[docs]</a>    <span class="k">def</span> <span class="nf">create_graph_from_data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Run the algorithm on data.</span>

<span class="sd">        Args:</span>
<span class="sd">            data (pandas.DataFrame): DataFrame containing the data</span>

<span class="sd">        Returns:</span>
<span class="sd">            networkx.DiGraph: Solution given by the algorithm.</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># Building setup w/ arguments.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">arguments</span><span class="p">[</span><span class="s1">&#39;</span><span class="si">{SCORE}</span><span class="s1">&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">score</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">arguments</span><span class="p">[</span><span class="s1">&#39;</span><span class="si">{VERBOSE}</span><span class="s1">&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">verbose</span><span class="p">)</span><span class="o">.</span><span class="n">upper</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">arguments</span><span class="p">[</span><span class="s1">&#39;</span><span class="si">{BETA}</span><span class="s1">&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">beta</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">arguments</span><span class="p">[</span><span class="s1">&#39;</span><span class="si">{OPTIM}</span><span class="s1">&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">optim</span><span class="p">)</span><span class="o">.</span><span class="n">upper</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">arguments</span><span class="p">[</span><span class="s1">&#39;</span><span class="si">{ALPHA}</span><span class="s1">&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">alpha</span><span class="p">)</span>

        <span class="n">cols</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">columns</span><span class="p">)</span>
        <span class="n">data2</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
        <span class="n">data2</span><span class="o">.</span><span class="n">columns</span> <span class="o">=</span> <span class="p">[</span><span class="n">i</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">])]</span>
        <span class="n">results</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_run_bnlearn</span><span class="p">(</span><span class="n">data2</span><span class="p">,</span> <span class="n">verbose</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">verbose</span><span class="p">)</span>
        <span class="n">graph</span> <span class="o">=</span> <span class="n">nx</span><span class="o">.</span><span class="n">DiGraph</span><span class="p">()</span>
        <span class="n">graph</span><span class="o">.</span><span class="n">add_nodes_from</span><span class="p">([</span><span class="s1">&#39;X&#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">])])</span>
        <span class="n">graph</span><span class="o">.</span><span class="n">add_edges_from</span><span class="p">(</span><span class="n">results</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">nx</span><span class="o">.</span><span class="n">relabel_nodes</span><span class="p">(</span><span class="n">graph</span><span class="p">,</span> <span class="p">{</span><span class="n">i</span><span class="p">:</span> <span class="n">j</span> <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">j</span> <span class="ow">in</span>
                                        <span class="nb">zip</span><span class="p">([</span><span class="s1">&#39;X&#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span>
                                             <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">])],</span> <span class="n">cols</span><span class="p">)})</span></div>

    <span class="k">def</span> <span class="nf">_run_bnlearn</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">whitelist</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">blacklist</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">verbose</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Setting up and running bnlearn with all arguments.&quot;&quot;&quot;</span>
        <span class="c1"># Run the algorithm</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">arguments</span><span class="p">[</span><span class="s1">&#39;</span><span class="si">{FOLDER}</span><span class="s1">&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">Path</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">{0!s}</span><span class="s1">/cdt_bnlearn_</span><span class="si">{1!s}</span><span class="s1">/&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">gettempdir</span><span class="p">(),</span> <span class="n">uuid</span><span class="o">.</span><span class="n">uuid4</span><span class="p">()))</span>
        <span class="n">run_dir</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">arguments</span><span class="p">[</span><span class="s1">&#39;</span><span class="si">{FOLDER}</span><span class="s1">&#39;</span><span class="p">]</span>
        <span class="n">os</span><span class="o">.</span><span class="n">makedirs</span><span class="p">(</span><span class="n">run_dir</span><span class="p">,</span> <span class="n">exist_ok</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">retrieve_result</span><span class="p">():</span>
            <span class="k">return</span> <span class="n">read_csv</span><span class="p">(</span><span class="n">Path</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">{}</span><span class="s1">/result.csv&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">run_dir</span><span class="p">)),</span> <span class="n">delimiter</span><span class="o">=</span><span class="s1">&#39;,&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">values</span>

        <span class="k">try</span><span class="p">:</span>
            <span class="n">data</span><span class="o">.</span><span class="n">to_csv</span><span class="p">(</span><span class="n">Path</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">{}</span><span class="s1">/data.csv&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">run_dir</span><span class="p">)),</span> <span class="n">index</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">blacklist</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">blacklist</span><span class="o">.</span><span class="n">to_csv</span><span class="p">(</span><span class="n">Path</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">{}</span><span class="s1">/blacklist.csv&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">run_dir</span><span class="p">)),</span> <span class="n">index</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">header</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">arguments</span><span class="p">[</span><span class="s1">&#39;</span><span class="si">{E_BLACKL}</span><span class="s1">&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;TRUE&#39;</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">arguments</span><span class="p">[</span><span class="s1">&#39;</span><span class="si">{E_BLACKL}</span><span class="s1">&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;FALSE&#39;</span>

            <span class="k">if</span> <span class="n">whitelist</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">whitelist</span><span class="o">.</span><span class="n">to_csv</span><span class="p">(</span><span class="n">Path</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">{}</span><span class="s1">/whitelist.csv&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">run_dir</span><span class="p">)),</span> <span class="n">index</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">header</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">arguments</span><span class="p">[</span><span class="s1">&#39;</span><span class="si">{E_WHITEL}</span><span class="s1">&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;TRUE&#39;</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">arguments</span><span class="p">[</span><span class="s1">&#39;</span><span class="si">{E_WHITEL}</span><span class="s1">&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;FALSE&#39;</span>

            <span class="n">bnlearn_result</span> <span class="o">=</span> <span class="n">launch_R_script</span><span class="p">(</span><span class="n">Path</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">{}</span><span class="s2">/R_templates/bnlearn.R&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">realpath</span><span class="p">(</span><span class="vm">__file__</span><span class="p">)))),</span>
                                             <span class="bp">self</span><span class="o">.</span><span class="n">arguments</span><span class="p">,</span> <span class="n">output_function</span><span class="o">=</span><span class="n">retrieve_result</span><span class="p">,</span> <span class="n">verbose</span><span class="o">=</span><span class="n">verbose</span><span class="p">)</span>
        <span class="c1"># Cleanup</span>
        <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="n">rmtree</span><span class="p">(</span><span class="n">run_dir</span><span class="p">)</span>
            <span class="k">raise</span> <span class="n">e</span>
        <span class="k">except</span> <span class="ne">KeyboardInterrupt</span><span class="p">:</span>
            <span class="n">rmtree</span><span class="p">(</span><span class="n">run_dir</span><span class="p">)</span>
            <span class="k">raise</span> <span class="ne">KeyboardInterrupt</span>
        <span class="n">rmtree</span><span class="p">(</span><span class="n">run_dir</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">bnlearn_result</span></div>


<div class="viewcode-block" id="GS"><a class="viewcode-back" href="../../../../causality.html#cdt.causality.graph.bnlearn.GS">[docs]</a><span class="k">class</span> <span class="nc">GS</span><span class="p">(</span><span class="n">BNlearnAlgorithm</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Grow-Shrink algorithm.</span>

<span class="sd">    **Description:** The Grow Shrink algorithm is a constraint based algorithm</span>
<span class="sd">    to recover bayesian networks. It consists in two phases, one growing phase</span>
<span class="sd">    in which nodes are added to the markov blanket based on conditional</span>
<span class="sd">    independence and a shrinking phase in which most irrelevant nodes are</span>
<span class="sd">    removed.</span>

<span class="sd">    **Required R packages**: bnlearn</span>

<span class="sd">    **Data Type:** Depends on the test used. Check</span>
<span class="sd">    :ref:`here &lt;bnlearntests&gt;` for the list of available tests.</span>

<span class="sd">    **Assumptions:** GS outputs a CPDAG, with additional assumptions depending</span>
<span class="sd">    on the conditional test used.</span>

<span class="sd">    .. note::</span>
<span class="sd">       Margaritis D (2003).</span>
<span class="sd">       Learning Bayesian Network Model Structure from Data</span>
<span class="sd">       . Ph.D. thesis, School</span>
<span class="sd">       of Computer Science, Carnegie-Mellon University, Pittsburgh, PA. Available as Technical Report</span>
<span class="sd">       CMU-CS-03-153</span>

<span class="sd">    Example:</span>
<span class="sd">        &gt;&gt;&gt; import networkx as nx</span>
<span class="sd">        &gt;&gt;&gt; from cdt.causality.graph import GS</span>
<span class="sd">        &gt;&gt;&gt; from cdt.data import load_dataset</span>
<span class="sd">        &gt;&gt;&gt; data, graph = load_dataset(&quot;sachs&quot;)</span>
<span class="sd">        &gt;&gt;&gt; obj = GS()</span>
<span class="sd">        &gt;&gt;&gt; #The predict() method works without a graph, or with a</span>
<span class="sd">        &gt;&gt;&gt; #directed or undirected graph provided as an input</span>
<span class="sd">        &gt;&gt;&gt; output = obj.predict(data)    #No graph provided as an argument</span>
<span class="sd">        &gt;&gt;&gt;</span>
<span class="sd">        &gt;&gt;&gt; output = obj.predict(data, nx.Graph(graph))  #With an undirected graph</span>
<span class="sd">        &gt;&gt;&gt;</span>
<span class="sd">        &gt;&gt;&gt; output = obj.predict(data, graph)  #With a directed graph</span>
<span class="sd">        &gt;&gt;&gt;</span>
<span class="sd">        &gt;&gt;&gt; #To view the graph created, run the below commands:</span>
<span class="sd">        &gt;&gt;&gt; nx.draw_networkx(output, font_size=8)</span>
<span class="sd">        &gt;&gt;&gt; plt.show()</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Init the model.&quot;&quot;&quot;</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">GS</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">arguments</span><span class="p">[</span><span class="s1">&#39;</span><span class="si">{ALGORITHM}</span><span class="s1">&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;gs&#39;</span></div>


<div class="viewcode-block" id="IAMB"><a class="viewcode-back" href="../../../../causality.html#cdt.causality.graph.bnlearn.IAMB">[docs]</a><span class="k">class</span> <span class="nc">IAMB</span><span class="p">(</span><span class="n">BNlearnAlgorithm</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;IAMB algorithm.</span>

<span class="sd">    **Description:** The is a bayesian constraint based algorithm</span>
<span class="sd">    to recover Markov blankets in a forward selection and a modified backward</span>
<span class="sd">    selection process.</span>

<span class="sd">    **Required R packages**: bnlearn</span>

<span class="sd">    **Data Type:** Depends on the test used. Check</span>
<span class="sd">    :ref:`here &lt;bnlearntests&gt;` for the list of available tests.</span>

<span class="sd">    **Assumptions:** IAMB outputs Markov blankets of nodes,</span>
<span class="sd">    with additional assumptions depending on the conditional test used.</span>

<span class="sd">    .. note::</span>
<span class="sd">       Tsamardinos  I,  Aliferis  CF,  Statnikov  A  (2003).   &quot;Algorithms  for  Large  Scale  Markov  Blanket</span>
<span class="sd">       Discovery&quot;.  In &quot;Proceedings of the Sixteenth International Florida Artificial Intelligence Research</span>
<span class="sd">       Society Conference&quot;, pp. 376-381. AAAI Press.</span>

<span class="sd">    Example:</span>
<span class="sd">        &gt;&gt;&gt; import networkx as nx</span>
<span class="sd">        &gt;&gt;&gt; from cdt.causality.graph import IAMB</span>
<span class="sd">        &gt;&gt;&gt; from cdt.data import load_dataset</span>
<span class="sd">        &gt;&gt;&gt; data, graph = load_dataset(&quot;sachs&quot;)</span>
<span class="sd">        &gt;&gt;&gt; obj = IAMB()</span>
<span class="sd">        &gt;&gt;&gt; #The predict() method works without a graph, or with a</span>
<span class="sd">        &gt;&gt;&gt; #directed or undirected graph provided as an input</span>
<span class="sd">        &gt;&gt;&gt; output = obj.predict(data)    #No graph provided as an argument</span>
<span class="sd">        &gt;&gt;&gt;</span>
<span class="sd">        &gt;&gt;&gt; output = obj.predict(data, nx.Graph(graph))  #With an undirected graph</span>
<span class="sd">        &gt;&gt;&gt;</span>
<span class="sd">        &gt;&gt;&gt; output = obj.predict(data, graph)  #With a directed graph</span>
<span class="sd">        &gt;&gt;&gt;</span>
<span class="sd">        &gt;&gt;&gt; #To view the graph created, run the below commands:</span>
<span class="sd">        &gt;&gt;&gt; nx.draw_networkx(output, font_size=8)</span>
<span class="sd">        &gt;&gt;&gt; plt.show()</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Init the model.&quot;&quot;&quot;</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">IAMB</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">arguments</span><span class="p">[</span><span class="s1">&#39;</span><span class="si">{ALGORITHM}</span><span class="s1">&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;iamb&#39;</span></div>


<div class="viewcode-block" id="Fast_IAMB"><a class="viewcode-back" href="../../../../causality.html#cdt.causality.graph.bnlearn.Fast_IAMB">[docs]</a><span class="k">class</span> <span class="nc">Fast_IAMB</span><span class="p">(</span><span class="n">BNlearnAlgorithm</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Fast IAMB algorithm.</span>

<span class="sd">    **Description:** Similar to IAMB, Fast-IAMB adds speculation to provide more</span>
<span class="sd">    computational performance without affecting the accuracy of markov blanket</span>
<span class="sd">    recovery.</span>

<span class="sd">    **Required R packages**: bnlearn</span>

<span class="sd">    **Data Type:** Depends on the test used. Check</span>
<span class="sd">    :ref:`here &lt;bnlearntests&gt;` for the list of available tests.</span>

<span class="sd">    **Assumptions:** Fast-IAMB outputs markov blankets of nodes, with additional</span>
<span class="sd">    assumptions depending on the conditional test used.</span>

<span class="sd">    .. note::</span>
<span class="sd">        Yaramakala S, Margaritis D (2005).  &quot;Speculative Markov Blanket Discovery for Optimal Feature</span>
<span class="sd">        Selection&quot;.  In &quot;ICDM ’05:  Proceedings of the Fifth IEEE International Conference on Data</span>
<span class="sd">        Mining&quot;, pp. 809-812. IEEE Computer Society.</span>

<span class="sd">    Example:</span>
<span class="sd">        &gt;&gt;&gt; import networkx as nx</span>
<span class="sd">        &gt;&gt;&gt; from cdt.causality.graph import Fast_IAMB</span>
<span class="sd">        &gt;&gt;&gt; from cdt.data import load_dataset</span>
<span class="sd">        &gt;&gt;&gt; data, graph = load_dataset(&quot;sachs&quot;)</span>
<span class="sd">        &gt;&gt;&gt; obj = Fast_IAMB()</span>
<span class="sd">        &gt;&gt;&gt; #The predict() method works without a graph, or with a</span>
<span class="sd">        &gt;&gt;&gt; #directed or undirected graph provided as an input</span>
<span class="sd">        &gt;&gt;&gt; output = obj.predict(data)    #No graph provided as an argument</span>
<span class="sd">        &gt;&gt;&gt;</span>
<span class="sd">        &gt;&gt;&gt; output = obj.predict(data, nx.Graph(graph))  #With an undirected graph</span>
<span class="sd">        &gt;&gt;&gt;</span>
<span class="sd">        &gt;&gt;&gt; output = obj.predict(data, graph)  #With a directed graph</span>
<span class="sd">        &gt;&gt;&gt;</span>
<span class="sd">        &gt;&gt;&gt; #To view the graph created, run the below commands:</span>
<span class="sd">        &gt;&gt;&gt; nx.draw_networkx(output, font_size=8)</span>
<span class="sd">        &gt;&gt;&gt; plt.show()</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Init the model.&quot;&quot;&quot;</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">Fast_IAMB</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">arguments</span><span class="p">[</span><span class="s1">&#39;</span><span class="si">{ALGORITHM}</span><span class="s1">&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;fast.iamb&#39;</span></div>


<div class="viewcode-block" id="Inter_IAMB"><a class="viewcode-back" href="../../../../causality.html#cdt.causality.graph.bnlearn.Inter_IAMB">[docs]</a><span class="k">class</span> <span class="nc">Inter_IAMB</span><span class="p">(</span><span class="n">BNlearnAlgorithm</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Interleaved IAMB algorithm.</span>

<span class="sd">    **Description:** Similar to IAMB, Interleaved-IAMB has a progressive</span>
<span class="sd">    forward selection minimizing false positives.</span>

<span class="sd">    **Required R packages**: bnlearn</span>

<span class="sd">    **Data Type:** Depends on the test used. Check</span>
<span class="sd">    :ref:`here &lt;bnlearntests&gt;` for the list of available tests.</span>

<span class="sd">    **Assumptions:** Inter-IAMB outputs markov blankets of nodes, with additional</span>
<span class="sd">    assumptions depending on the conditional test used.</span>

<span class="sd">    .. note::</span>
<span class="sd">       Yaramakala S, Margaritis D (2005).  &quot;Speculative Markov Blanket Discovery for Optimal Feature</span>
<span class="sd">       Selection&quot;.  In &quot;ICDM ’05:  Proceedings of the Fifth IEEE International Conference on Data Min-</span>
<span class="sd">       ing&quot;, pp. 809-812. IEEE Computer Society.</span>

<span class="sd">    Example:</span>
<span class="sd">        &gt;&gt;&gt; import networkx as nx</span>
<span class="sd">        &gt;&gt;&gt; from cdt.causality.graph import Inter_IAMB</span>
<span class="sd">        &gt;&gt;&gt; from cdt.data import load_dataset</span>
<span class="sd">        &gt;&gt;&gt; data, graph = load_dataset(&quot;sachs&quot;)</span>
<span class="sd">        &gt;&gt;&gt; obj = Inter_IAMB()</span>
<span class="sd">        &gt;&gt;&gt; #The predict() method works without a graph, or with a</span>
<span class="sd">        &gt;&gt;&gt; #directed or undirected graph provided as an input</span>
<span class="sd">        &gt;&gt;&gt; output = obj.predict(data)    #No graph provided as an argument</span>
<span class="sd">        &gt;&gt;&gt;</span>
<span class="sd">        &gt;&gt;&gt; output = obj.predict(data, nx.Graph(graph))  #With an undirected graph</span>
<span class="sd">        &gt;&gt;&gt;</span>
<span class="sd">        &gt;&gt;&gt; output = obj.predict(data, graph)  #With a directed graph</span>
<span class="sd">        &gt;&gt;&gt;</span>
<span class="sd">        &gt;&gt;&gt; #To view the graph created, run the below commands:</span>
<span class="sd">        &gt;&gt;&gt; nx.draw_networkx(output, font_size=8)</span>
<span class="sd">        &gt;&gt;&gt; plt.show()</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Init the model.&quot;&quot;&quot;</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">Inter_IAMB</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">arguments</span><span class="p">[</span><span class="s1">&#39;</span><span class="si">{ALGORITHM}</span><span class="s1">&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;inter.iamb&#39;</span></div>


<div class="viewcode-block" id="MMPC"><a class="viewcode-back" href="../../../../causality.html#cdt.causality.graph.bnlearn.MMPC">[docs]</a><span class="k">class</span> <span class="nc">MMPC</span><span class="p">(</span><span class="n">BNlearnAlgorithm</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Max-Min Parents-Children algorithm.</span>

<span class="sd">    **Description:** The Max-Min Parents-Children (MMPC) is a 2-phase algorithm</span>
<span class="sd">    with a forward pass and a backward pass. The forward phase adds recursively</span>
<span class="sd">    the variables that possess the highest association with the target</span>
<span class="sd">    conditionally to the already selected variables. The backward pass tests</span>
<span class="sd">    d-separability of variables conditionally to the set and subsets of the</span>
<span class="sd">    selected variables.</span>

<span class="sd">    **Required R packages**: bnlearn</span>

<span class="sd">    **Data Type:** Depends on the test used. Check</span>
<span class="sd">    :ref:`here &lt;bnlearntests&gt;` for the list of available tests.</span>

<span class="sd">    **Assumptions:** MMPC outputs markov blankets of nodes, with additional</span>
<span class="sd">    assumptions depending on the conditional test used.</span>

<span class="sd">    .. note::</span>
<span class="sd">       Tsamardinos I, Aliferis CF, Statnikov A (2003). &quot;Time and Sample Efficient Discovery of Markov</span>
<span class="sd">       Blankets and Direct Causal Relations&quot;.  In &quot;KDD ’03:  Proceedings of the Ninth ACM SIGKDD</span>
<span class="sd">       International Conference on Knowledge Discovery and Data Mining&quot;, pp. 673-678. ACM.</span>
<span class="sd">       Tsamardinos I, Brown LE, Aliferis CF (2006).  &quot;The Max-Min Hill-Climbing Bayesian Network</span>
<span class="sd">       Structure Learning Algorithm&quot;.</span>
<span class="sd">       Machine Learning,65(1), 31-78.</span>

<span class="sd">    Example:</span>
<span class="sd">        &gt;&gt;&gt; import networkx as nx</span>
<span class="sd">        &gt;&gt;&gt; from cdt.causality.graph import MMPC</span>
<span class="sd">        &gt;&gt;&gt; from cdt.data import load_dataset</span>
<span class="sd">        &gt;&gt;&gt; data, graph = load_dataset(&quot;sachs&quot;)</span>
<span class="sd">        &gt;&gt;&gt; obj = MMPC()</span>
<span class="sd">        &gt;&gt;&gt; #The predict() method works without a graph, or with a</span>
<span class="sd">        &gt;&gt;&gt; #directed or undirected graph provided as an input</span>
<span class="sd">        &gt;&gt;&gt; output = obj.predict(data)    #No graph provided as an argument</span>
<span class="sd">        &gt;&gt;&gt;</span>
<span class="sd">        &gt;&gt;&gt; output = obj.predict(data, nx.Graph(graph))  #With an undirected graph</span>
<span class="sd">        &gt;&gt;&gt;</span>
<span class="sd">        &gt;&gt;&gt; output = obj.predict(data, graph)  #With a directed graph</span>
<span class="sd">        &gt;&gt;&gt;</span>
<span class="sd">        &gt;&gt;&gt; #To view the graph created, run the below commands:</span>
<span class="sd">        &gt;&gt;&gt; nx.draw_networkx(output, font_size=8)</span>
<span class="sd">        &gt;&gt;&gt; plt.show()</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Init the model.&quot;&quot;&quot;</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">MMPC</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">arguments</span><span class="p">[</span><span class="s1">&#39;</span><span class="si">{ALGORITHM}</span><span class="s1">&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;mmpc&#39;</span></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        
        &copy; Copyright 2018, Diviyan Kalainathan, Olivier Goudet

    </p>
  </div>
    
    
    
    Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a
    
    <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a>
    
    provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>